//
//  SHomeViewController.m
//  PocketFM
//
//  Created by sun on 15/12/16.
//  Copyright © 2015年 Sun. All rights reserved.
//

#import "SHomeViewController.h"
#import "IconTableViewCell.h"
#import "SHotPlayerTableViewCell.h"
#import "STopTableViewCell.h"
#import "STwoTableViewCell.h"
#import "SManagerTableViewCell.h"
#import "SFiveTableViewCell.h"
#import "STextTableViewCell.h"
#import "STextTableViewCell2.h"
#import "ListCategoryModel.h"
#import <MJRefresh.h>
#import "UrlHelper.h"
#import "SDCycleScrollView.h"
#import "UrlAPI.pch"

@interface SHomeViewController ()<UITableViewDataSource, UITableViewDelegate, SDCycleScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) UrlHelper *helper;
@property (nonatomic, strong) NSArray *dataArray;
@end

@implementation SHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = NO;
    
    self.helper = [[UrlHelper alloc] init];
    [self.helper getDictionaryWithUrl:HomeHead urlBody:HomeBody success:^(NSArray *modelArray) {
        self.dataArray= [NSArray arrayWithArray:modelArray];
        [self.tableView reloadData];
    } fail:nil];
    [self setupCycleScrollView];
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        STopTableViewCell *cell = [STopTableViewCell cellWithTabelView:tableView];
        return cell;
    }else if(indexPath.section == 1){
        SHotPlayerTableViewCell *cell = [SHotPlayerTableViewCell cellWithTabelView:tableView];
        cell.model = self.dataArray[0];
        return cell;
    }else if(indexPath.section == 2){
        SHotPlayerTableViewCell *cell = [SHotPlayerTableViewCell cellWithTabelView:tableView];
        cell.model = self.dataArray[1];
        return cell;
    }else if (indexPath.section == 3 && indexPath.row == 0){
        SFiveTableViewCell *cell = [SFiveTableViewCell cellWithTabelView:tableView];
        cell.model = self.dataArray[2];
        return cell;
    }else if(indexPath.section == 3 && indexPath.row == 1){
        STextTableViewCell *cell = [STextTableViewCell cellWithTabelView:tableView];
        cell.model = self.dataArray[2];
        return cell;
    }else if(indexPath.section == 3 && indexPath.row == 2){
        STextTableViewCell2 *cell = [STextTableViewCell2 cellWithTabelView:tableView];
         cell.model2 = self.dataArray[2];
        return cell;
    }else if(indexPath.section == 4){
        SHotPlayerTableViewCell *cell = [SHotPlayerTableViewCell cellWithTabelView:tableView];
        cell.model = self.dataArray[3];
        return cell;
    }else if(indexPath.section == 5){
        SHotPlayerTableViewCell *cell = [SHotPlayerTableViewCell cellWithTabelView:tableView];
        cell.model = self.dataArray[4];
        return cell;
    }else if(indexPath.section > 5 && indexPath.section < 29){
        STwoTableViewCell *cell = [STwoTableViewCell cellWithTabelView:tableView];
        cell.model = self.dataArray[indexPath.section - 1];
        return cell;
    }else{
        SHotPlayerTableViewCell *cell = [SHotPlayerTableViewCell cellWithTabelView:tableView];
        cell.model = self.dataArray[indexPath.section];
        return cell;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 3) {
        return 3;
    }
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 48;
    }else if (indexPath.section > 5){
        return 153;
    }else if (indexPath.section == 29){
        return 42;
    }else if (indexPath.section == 3 && indexPath.row == 0){
        return 147;
    }else if (indexPath.section == 3 && (indexPath.row == 1 || indexPath.row == 2)){
        return 63;
    }
    return 160;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
 
    return 6;
}
- (void)setupCycleScrollView {
    [self.helper getScrollImageWithUrl:ScrollHead urlBody:nil success:^(NSArray *array) {
        /** 轮播图 */
        CGFloat Width = self.view.bounds.size.width;
        CGFloat Height = 135;
        SDCycleScrollView *cycleView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, Width, Height) imageURLStringsGroup:nil];
        cycleView.delegate = self;
        cycleView.dotColor = [UIColor whiteColor];
        self.tableView.tableHeaderView = cycleView;
        cycleView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
        cycleView.showPageControl = NO;
        //延迟加载
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            cycleView.imageURLStringsGroup = array;
        });    } fail:nil];

}
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"---点击了第%ld张图片", index);
    // 需要push到下一界面
    
}
@end
